/*     */ package com.zimbra.cs.dav.client;
/*     */ 
/*     */ import com.zimbra.common.util.Pair;
/*     */ import com.zimbra.cs.dav.DavContext.Depth;
/*     */ import com.zimbra.cs.dav.DavElements;
/*     */ import java.io.ByteArrayOutputStream;
/*     */ import java.io.IOException;
/*     */ import java.io.OutputStream;
/*     */ import java.util.ArrayList;
/*     */ import org.apache.commons.httpclient.HttpMethod;
/*     */ import org.apache.commons.httpclient.methods.GetMethod;
/*     */ import org.apache.commons.httpclient.methods.PutMethod;
/*     */ import org.apache.commons.httpclient.methods.RequestEntity;
/*     */ import org.dom4j.Document;
/*     */ import org.dom4j.DocumentHelper;
/*     */ import org.dom4j.Element;
/*     */ import org.dom4j.QName;
/*     */ import org.dom4j.io.OutputFormat;
/*     */ import org.dom4j.io.XMLWriter;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class DavRequest
/*     */ {
/*     */   private Document mDoc;
/*     */   private String mUri;
/*     */   private String mRedirectUrl;
/*     */   private String mMethod;
/*     */   private DavContext.Depth mDepth;
/*     */   private ArrayList<Pair<String, String>> mHeaders;
/*     */   private static final String PROPFIND = "PROPFIND";
/*     */   private static final String REPORT = "REPORT";
/*     */   private static final String DELETE = "DELETE";
/*     */   private static final String MKCOL = "MKCOL";
/*     */   private static final String MKCALENDAR = "MKCALENDAR";
/*     */   private static final String PROPPATCH = "PROPPATCH";
/*     */   private static final String OPTION = "OPTION";
/*     */   
/*     */   public DavRequest(String uri, String method)
/*     */   {
/*  42 */     this.mUri = uri;
/*  43 */     this.mMethod = method;
/*  44 */     this.mDepth = DavContext.Depth.zero;
/*  45 */     this.mHeaders = new ArrayList();
/*     */   }
/*     */   
/*     */   public Document getRequestMessage() {
/*  49 */     return this.mDoc;
/*     */   }
/*     */   
/*     */   public void setRedirectUrl(String url) {
/*  53 */     this.mRedirectUrl = url;
/*     */   }
/*     */   
/*     */   public void setRequestMessage(Element root) {
/*  57 */     if (this.mDoc == null)
/*  58 */       this.mDoc = DocumentHelper.createDocument();
/*  59 */     this.mDoc.setRootElement(root);
/*     */   }
/*     */   
/*     */   public void addRequestProp(QName p) {
/*  63 */     if (this.mDoc == null)
/*  64 */       return;
/*  65 */     Element prop = this.mDoc.getRootElement().element(DavElements.E_PROP);
/*  66 */     if (prop == null)
/*  67 */       return;
/*  68 */     prop.addElement(p);
/*     */   }
/*     */   
/*     */   public void addHref(String href) {
/*  72 */     if (this.mDoc == null)
/*  73 */       return;
/*  74 */     Element el = this.mDoc.getRootElement().addElement(DavElements.E_HREF);
/*  75 */     el.setText(href);
/*     */   }
/*     */   
/*     */   public void addRequestElement(Element e) {
/*  79 */     if (this.mDoc == null)
/*  80 */       return;
/*  81 */     this.mDoc.getRootElement().add(e);
/*     */   }
/*     */   
/*     */   public void addRequestHeader(String name, String value) {
/*  85 */     this.mHeaders.add(new Pair(name, value));
/*     */   }
/*     */   
/*     */   public ArrayList<Pair<String, String>> getRequestHeaders() {
/*  89 */     return this.mHeaders;
/*     */   }
/*     */   
/*     */   public void setDepth(DavContext.Depth d) {
/*  93 */     this.mDepth = d;
/*     */   }
/*     */   
/*     */   public DavContext.Depth getDepth() {
/*  97 */     return this.mDepth;
/*     */   }
/*     */   
/*     */   public String getUri() {
/* 101 */     return this.mUri;
/*     */   }
/*     */   
/* 104 */   public String getMethod() { return this.mMethod; }
/*     */   
/*     */   public String getRequestMessageString() throws IOException {
/* 107 */     if (this.mDoc != null) {
/* 108 */       OutputFormat format = OutputFormat.createPrettyPrint();
/* 109 */       format.setTrimText(false);
/* 110 */       format.setOmitEncoding(false);
/* 111 */       ByteArrayOutputStream out = new ByteArrayOutputStream();
/* 112 */       XMLWriter writer = new XMLWriter(out, format);
/* 113 */       writer.write(this.mDoc);
/* 114 */       return new String(out.toByteArray(), "UTF-8");
/*     */     }
/* 116 */     return "";
/*     */   }
/*     */   
/*     */   public static class DocumentRequestEntity implements RequestEntity { private Document doc;
/*     */     private byte[] buffer;
/*     */     
/* 122 */     public DocumentRequestEntity(Document d) { this.doc = d;this.buffer = null; }
/* 123 */     public boolean isRepeatable() { return true; }
/*     */     
/* 125 */     public long getContentLength() { if (this.buffer == null) {
/*     */         try {
/* 127 */           getContents();
/*     */         } catch (Exception e) {}
/*     */       }
/* 130 */       if (this.buffer == null) return -1L;
/* 131 */       return this.buffer.length; }
/*     */     
/* 133 */     public String getContentType() { return "text/xml"; }
/*     */     
/* 135 */     public void writeRequest(OutputStream out) throws IOException { if (this.buffer != null) {
/* 136 */         out.write(this.buffer);
/* 137 */         return;
/*     */       }
/* 139 */       OutputFormat format = OutputFormat.createCompactFormat();
/* 140 */       format.setTrimText(false);
/* 141 */       format.setOmitEncoding(false);
/* 142 */       XMLWriter writer = new XMLWriter(out, format);
/* 143 */       writer.write(this.doc);
/*     */     }
/*     */     
/* 146 */     private void getContents() throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream();
/* 147 */       writeRequest(out);
/* 148 */       this.buffer = out.toByteArray();
/*     */     }
/*     */   }
/*     */   
/*     */   public HttpMethod getHttpMethod(String baseUrl) {
/* 153 */     String url = this.mRedirectUrl;
/* 154 */     if (url == null) {
/* 155 */       url = baseUrl + this.mUri;
/*     */     }
/* 157 */     if (this.mDoc == null) {
/* 158 */       new GetMethod(url) {
/*     */         public String getName() {
/* 160 */           return DavRequest.this.mMethod;
/*     */         }
/*     */       };
/*     */     }
/* 164 */     PutMethod m = new PutMethod(url) {
/*     */       RequestEntity re;
/*     */       
/* 167 */       public String getName() { return DavRequest.this.mMethod; }
/*     */       
/*     */ 
/* 170 */       protected RequestEntity generateRequestEntity() { return this.re; }
/*     */       
/*     */       public void setRequestEntity(RequestEntity requestEntity) {
/* 173 */         this.re = requestEntity;
/* 174 */         super.setRequestEntity(requestEntity);
/*     */       }
/* 176 */     };
/* 177 */     DocumentRequestEntity re = new DocumentRequestEntity(this.mDoc);
/* 178 */     m.setRequestEntity(re);
/* 179 */     return m;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static DavRequest PROPFIND(String uri)
/*     */   {
/* 198 */     DavRequest req = new DavRequest(uri, "PROPFIND");
/* 199 */     Element root = DocumentHelper.createElement(DavElements.E_PROPFIND);
/* 200 */     root.addElement(DavElements.E_PROP);
/* 201 */     req.setRequestMessage(root);
/* 202 */     return req;
/*     */   }
/*     */   
/*     */   public static DavRequest CALENDARMULTIGET(String uri) {
/* 206 */     DavRequest req = new DavRequest(uri, "REPORT");
/* 207 */     Element root = DocumentHelper.createElement(DavElements.E_CALENDAR_MULTIGET);
/* 208 */     root.addElement(DavElements.E_PROP);
/* 209 */     req.setRequestMessage(root);
/* 210 */     return req;
/*     */   }
/*     */   
/*     */   public static DavRequest EXPAND(String uri) {
/* 214 */     DavRequest req = new DavRequest(uri, "REPORT");
/* 215 */     Element root = DocumentHelper.createElement(DavElements.E_EXPAND_PROPERTY);
/* 216 */     req.setRequestMessage(root);
/* 217 */     return req;
/*     */   }
/*     */   
/*     */   public static DavRequest CALENDARQUERY(String uri) {
/* 221 */     DavRequest req = new DavRequest(uri, "REPORT");
/* 222 */     Element root = DocumentHelper.createElement(DavElements.E_CALENDAR_QUERY);
/* 223 */     root.addElement(DavElements.E_PROP);
/* 224 */     root.addElement(DavElements.E_FILTER).addElement(DavElements.E_COMP_FILTER).addAttribute("name", "VCALENDAR");
/* 225 */     req.setRequestMessage(root);
/* 226 */     return req;
/*     */   }
/*     */   
/*     */   public static DavRequest DELETE(String uri) {
/* 230 */     DavRequest req = new DavRequest(uri, "DELETE");
/* 231 */     return req;
/*     */   }
/*     */   
/*     */   public static DavRequest MKCOL(String uri) {
/* 235 */     DavRequest req = new DavRequest(uri, "MKCOL");
/* 236 */     return req;
/*     */   }
/*     */   
/*     */   public static DavRequest MKCALENDAR(String uri) {
/* 240 */     DavRequest req = new DavRequest(uri, "MKCALENDAR");
/* 241 */     return req;
/*     */   }
/*     */   
/*     */   public static DavRequest PROPPATCH(String uri) {
/* 245 */     DavRequest req = new DavRequest(uri, "PROPPATCH");
/* 246 */     Element root = DocumentHelper.createElement(DavElements.E_PROPERTYUPDATE);
/* 247 */     root.addElement(DavElements.E_SET).addElement(DavElements.E_PROP);
/* 248 */     req.setRequestMessage(root);
/* 249 */     return req;
/*     */   }
/*     */   
/*     */   public static DavRequest OPTION(String uri) {
/* 253 */     DavRequest req = new DavRequest(uri, "OPTION");
/* 254 */     return req;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/dav/client/DavRequest.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */